from urllib import request
import re

base_url = 'http://www.66ip.cn/%d.html'
f = open('66.csv','w',encoding='utf-8')

for i in range(1,101):
    fullurl = base_url % i
    print(fullurl)
    response = request.urlopen(fullurl)
    html = response.read().decode('gbk')

    # table正则
    table_pat = re.compile(r'<table.+?</table>',re.S)
    table = table_pat.findall(html)[-1]

    # tr_正则
    tr_pat = re.compile(r'<tr>.+?</tr>',re.S)
    tr_list = tr_pat.findall(table)

    # td_正则
    td_pat = re.compile(r'<td>(.+?)</td>')
    for tr in tr_list[1:]:
        td_list = td_pat.findall(tr)
        print(td_list)
        f.write(','.join(td_list) + '\n')

f.close()